﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using DAL = AccessDAL;
using CommonFunc;

namespace BLL
{
    public class File
    {
        DAL.FileTableAdapters.FileTableAdapter DF =
            new DAL.FileTableAdapters.FileTableAdapter();

        MODEL.File _MF = new MODEL.File();


        #region GetDateByID
        /// <summary>
        /// GetDateByID
        /// </summary>
        /// <param name="MF"></param>
        /// <returns></returns>
        public MODEL.File GetDateByID(MODEL.File MF)
        {
            try
            {
                DataTable DT = DF.GetDataByID(MF.File_ID);

                _MF.File_DateTime = Convert.ToDateTime(DT.Rows[0]["File_DateTime"]);
                _MF.File_Description = DT.Rows[0]["File_Description"].ToString();
                _MF.File_ID = Convert.ToInt32(DT.Rows[0]["File_ID"]);
                _MF.File_Title = DT.Rows[0]["File_Title"].ToString();
                _MF.File_URL = DT.Rows[0]["File_URL"].ToString();

                return _MF;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 添加文档
        /// <summary>
        /// 添加文档
        /// </summary>
        /// <param name="MF"></param>
        public void Add(MODEL.File MF)
        {
            try
            {
                DataTable DT = DF.GetDataByTitle(MF.File_Title);

                if (DT.Rows.Count > 0)
                {
                    throw new Exception("此文档名称已被使用过");
                }

                DF.Add(MF.File_Title,
                    MF.File_URL,
                    MF.File_Description,
                    MF.File_DateTime);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改文档描述
        /// <summary>
        /// 修改文档描述
        /// </summary>
        /// <param name="MF"></param>
        public void Mod(MODEL.File MF)
        {
            try
            {

                DF.Mod(MF.File_Title,
                    MF.File_Description,
                    MF.File_ID);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 删除文档
        /// <summary>
        /// 删除文档
        /// </summary>
        /// <param name="MF"></param>
        public void Del(MODEL.File MF)
        {
            try
            {
                _MF = GetDateByID(MF);

                DF.Del(MF.File_ID);

                CommonFunction.DelFile(
                    HttpContext.Current.Server.MapPath("~/") + "UploadFile/" + _MF.File_URL);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改文档文件
        /// <summary>
        /// 修改文档文件
        /// </summary>
        /// <param name="MF"></param>
        public void ModFileURLByID(MODEL.File MF)
        {
            try
            {
                _MF = GetDateByID(MF);

                DF.ModFileURLByID(MF.File_URL, MF.File_ID);

                CommonFunction.DelFile(
                    HttpContext.Current.Server.MapPath("~/") + "UploadFile/" + _MF.File_URL);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}
